package com.example.administrator.social.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.example.administrator.social.R;
import com.example.administrator.social.model.MessageData;
import com.example.administrator.social.model.User;
import com.example.administrator.social.util.AllenUtils;
import com.example.administrator.social.view.ClearEditText;
import com.example.administrator.social.view.LeftSlideView;

import java.util.List;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

/**
 * Created by Administrator on 2018/9/10 0010.
 */

public class MessageRecyclerAdapter extends RecyclerView.Adapter<MessageRecyclerAdapter.ViewHolder>implements LeftSlideView.IonSlidingButtonListener  {
    private List<MessageData> mData;
    private Context mContext;
    private LayoutInflater mInflater;
    private final static int HEAD_COUNT = 1;
    private final static int FOOT_COUNT = 1;
    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;
    private final static int TYPE_FOOTER = 2;

    private IonSlidingViewClickListener mIDeleteBtnClickListener;
    private IonSlidingViewClickListener mISetBtnClickListener;
    private LeftSlideView mMenu = null;


    public MessageCallBack mCallBack;


    public interface MessageCallBack{
        public void filterDataForList(String s);
    }

    public MessageRecyclerAdapter(Context context, List<MessageData> data,
                                  IonSlidingViewClickListener listener){
        mData = data;
        mContext = context;
        mInflater = LayoutInflater.from(context);

        // delete menu
        mIDeleteBtnClickListener =  listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_HEAD) {
            View mHeaderView = mInflater.inflate(R.layout.message_recycler_item_header,parent,false);
            return new HeadHolder(mHeaderView);
        }else{
            View inflate = mInflater.inflate( R.layout.message_recycler_item_layout, parent,false);
            return  new ViewHolder(inflate);
        }
    }

    public boolean isHead(int position){
        return HEAD_COUNT != 0 && position == 0;
    }


    public boolean isFoot(int position){
        return FOOT_COUNT != 0 && position == mData.size() + HEAD_COUNT;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //设置内容布局的宽为屏幕宽度
        //header
        if (holder instanceof HeadHolder){
            HeadHolder headHolder = (HeadHolder) holder;
            headHolder.bindHeaderData(holder);
        }else if(holder instanceof ViewHolder){
            position = position-HEAD_COUNT;
            //content
            holder.bindContentData(position);
        }
    }



    public void setCallBack(MessageCallBack callBack){
        mCallBack = callBack;
    }


    @Override
    public int getItemViewType(int position) {
        if (HEAD_COUNT != 0 && position == 0){ // 头部
            return TYPE_HEAD;
        }else{
            return TYPE_CONTENT;
        }
    }
    @Override
    public int getItemCount() {
        return mData.size()+HEAD_COUNT;
    }


    /**
     * content ViewHolder
     */
    public class ViewHolder extends RecyclerView.ViewHolder {
        //content
        private TextView mNameText;
        private TextView mDate;
        private TextView mLastMassage;
        private ImageView mHeaderImage;
        private TextView mUnreadCount;
        private QBadgeView mUnreadCountBadgeView;
        private Badge mBadge;
        public RelativeLayout mContentLayout;
        //menu
        private TextView mDeleteMessageItem;
        public ViewHolder(View itemView) {
            super(itemView);
            mHeaderImage = (ImageView) itemView.findViewById(R.id.message_recycler_header_image);
            mDate = (TextView) itemView.findViewById(R.id.message_recycler_time_text);
            mLastMassage = (TextView) itemView.findViewById(R.id.message_recycler_lastmessage_text);
            mNameText = (TextView) itemView.findViewById(R.id.message_recycler_name_text);
            mUnreadCount = (TextView) itemView.findViewById(R.id.unread_message_count);
            //menu
            mDeleteMessageItem = (TextView) itemView.findViewById(R.id.message_delete_item);
            //contentLayout
            mContentLayout = (RelativeLayout) itemView.findViewById(R.id.layout_content);
            if(itemView instanceof LeftSlideView) {
                ((LeftSlideView) itemView).setSlidingButtonListener(MessageRecyclerAdapter.this);
            }
        }

        public void bindContentData(final int position) {
            mNameText.setText(mData.get(position).getName());
            mLastMassage.setText(mData.get(position).getLastMessage());
            mDate.setText(mData.get(position).getDate());
            mHeaderImage.setImageResource(mData.get(position).getHeaderImage());
            mUnreadCountBadgeView = new QBadgeView(mContext);
            mBadge = mUnreadCountBadgeView.bindTarget(mUnreadCount);
            mBadge.setBadgeNumber(12);
            mBadge.setBadgeTextSize(8.0f,true);
            mUnreadCount.setAlpha(0.0f);

            mContentLayout.getLayoutParams().width = AllenUtils.getScreenWidth(mContext);

            //set content onclick
            mContentLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (menuIsOpen()) {
                        closeMenu();
                    } else {
                        mIDeleteBtnClickListener.onItemClick(v, position);
                    }

                }
            });
            //left deleteButton onclick
            mDeleteMessageItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mIDeleteBtnClickListener.onDeleteBtnClick(view, position);
                }
            });

        }
    }


    /**
     * Recycler Header
     */
    public class HeadHolder extends ViewHolder{
        ClearEditText mClearEditText;
        public HeadHolder(View itemView) {
            super(itemView);
            mClearEditText = (ClearEditText) itemView.findViewById(R.id.filter_edit);
        }
        public void bindHeaderData(ViewHolder holder){
            if(mClearEditText!=null){
                mClearEditText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        mCallBack.filterDataForList(s.toString());
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
            }
        }
    }

    /**
     * update dataList
     * @param list
     */
    public void updateList(List<MessageData> list){
        mData = list;
        notifyDataSetChanged();
    }


    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (LeftSlideView) view;
    }

    @Override
    public void onDownOrMove(LeftSlideView leftSlideView) {
        if (menuIsOpen()) {
            if (mMenu != leftSlideView) {
                closeMenu();
            }
        }
    }

    /**
     * delete item
     * @param position
     */
    public void removeData(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * close menu
     */
    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }

    /**
     * judge menu is open
     * @return
     */
    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }

    /**
     * 注册接口的方法：点击事件。在Mactivity.java实现这些方法。
     */
    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position);
        void onDeleteBtnClick(View view, int position);
    }

}
